package cn.ttxs.wq.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.vondear.rxtool.RxDeviceTool;
import com.vondear.rxtool.view.RxToast;

import java.util.HashMap;
import java.util.Map;

import cn.ttxs.lib.tool.ToolLog;
import cn.ttxs.wq.R;

/**
 * Created by dick on 2017/10/24.
 */

public class DateSelectDialog extends Dialog implements View.OnClickListener,
        CalendarView.OnCalendarMultiSelectListener,
        CalendarView.OnYearChangeListener,
        CalendarView.OnMonthChangeListener,
        CalendarView.OnCalendarSelectListener {
    private Context context;
    private boolean cancelable = false;
    private TextView tv_year;
    private TextView tv_date_detail;
    private TextView title;
    private CalendarView mCalendarView;
    private TextView tv_clear;
    private TextView tv_cancel;
    private TextView tv_sure;
    private OnDateSelectedListener onDateSelectedListener;
    private static final String[] weekArr = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    public void setOnDateSelectedListener(OnDateSelectedListener onDateSelectedListener) {
        this.onDateSelectedListener = onDateSelectedListener;
    }

    private Map<String, Calendar> selectedMap = new HashMap<>();
    private ImageView iv_last_month;
    private ImageView iv_next_month;

    public DateSelectDialog(@NonNull Context context) {
        this(context, R.style.testDialog);
        init(context);
    }


    public DateSelectDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.context = context;
        init(context);
    }

    public DateSelectDialog(@NonNull Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
        this.cancelable = cancelable;
        init(context);
    }

    private void init(Context context) {
        setCancelable(this.cancelable);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_multi_choose, null, false);
        initView(view);
        setView();
        setContentView(view);
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = (int) (RxDeviceTool.getScreenWidth(context) * 0.85f);
        this.getWindow().setAttributes(lp);
    }


    private void initView(View view) {
        title = (TextView) view.findViewById(R.id.title);
        mCalendarView = (CalendarView) view.findViewById(R.id.calendarView);
        tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_sure = (TextView) view.findViewById(R.id.tv_sure);
        tv_clear = (TextView) view.findViewById(R.id.tv_clear);
        tv_clear.setOnClickListener(this);
        tv_year = (TextView) view.findViewById(R.id.tv_year);
        tv_date_detail = (TextView) view.findViewById(R.id.tv_date_detail);
        iv_last_month = (ImageView) view.findViewById(R.id.iv_last_month);
        iv_next_month = (ImageView) view.findViewById(R.id.iv_next_month);
        iv_last_month.setOnClickListener(this);
        iv_next_month.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        tv_sure.setOnClickListener(this);
    }

    private void setView() {
        java.util.Calendar instance = java.util.Calendar.getInstance();
        int year = instance.get(java.util.Calendar.YEAR);
        int month = instance.get(java.util.Calendar.MONTH) + 1;
        int day = instance.get(java.util.Calendar.DAY_OF_MONTH);
        int dayOfWeek = instance.get(java.util.Calendar.DAY_OF_WEEK);
        title.setText(year + "年" + month + "月");
        tv_year.setText(year + "");
        tv_date_detail.setText(month + "月" + day + "日" + weekArr[dayOfWeek - 1]);
        mCalendarView.setRange(year - 5, 1, 1, year, month, day);
        mCalendarView.scrollToCurrent();
        //设置点击事件
        mCalendarView.setOnCalendarMultiSelectListener(this);
        mCalendarView.setOnYearChangeListener(this);
        mCalendarView.setOnCalendarSelectListener(this);
        mCalendarView.setOnMonthChangeListener(this);
    }

    public void resetCalendar() {
        if (mCalendarView != null) {
            mCalendarView.clearMultiSelect();
        }
        if (selectedMap != null) {
            selectedMap.clear();
        }
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (mCalendarView == null) {
            return;
        }
        if (id == R.id.tv_clear) {
            mCalendarView.clearMultiSelect();
        } else if (id == R.id.iv_next_month) {
            mCalendarView.scrollToNext();
        } else if (id == R.id.iv_last_month) {
            mCalendarView.scrollToPre();
        } else if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.tv_sure) {
            if (this.onDateSelectedListener != null) {
                if (selectedMap != null && selectedMap.isEmpty()) {
                    RxToast.showToastShort("请选择日期");
                    return;
                }
                onDateSelectedListener.onDateSelected(selectedMap);
            }
            dismiss();
        }
    }

    @Override
    public void onCalendarMultiSelectOutOfRange(Calendar calendar) {

    }

    @Override
    public void onMultiSelectOutOfSize(Calendar calendar, int maxSize) {
        RxToast.showToastShort("最多选择" + maxSize + "个日期");
    }

    @Override
    public void onCalendarMultiSelect(Calendar calendar, int curSize, int maxSize) {
        String key = String.format("%04d-%02d-%02d", calendar.getYear(), calendar.getMonth(), calendar.getDay());
        if (selectedMap.containsKey(key)) {
            selectedMap.remove(key);
        } else {
            selectedMap.put(key, calendar);
        }
    }

    @Override
    public void onCalendarOutOfRange(Calendar calendar) {
        ToolLog.w("date", "OutOfRange" + calendar);
    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {
        ToolLog.w("date", "  -- " + calendar.getYear() +
                "  --  " + calendar.getMonth() +
                "  -- " + calendar.getDay() +
                "  --  " + isClick + "  --   " + calendar.getScheme());
    }

    @Override
    public void onMonthChange(int year, int month) {
        title.setText(year + "年" + month + "月");
    }

    @Override
    public void onYearChange(int year) {
        title.setText(year + "年" + mCalendarView.getCurMonth() + "月");
    }

    public interface OnDateSelectedListener {
        public void onDateSelected(Map<String, Calendar> dateBeanMap);
    }
}
